package cn.eeepay.framework.service.unTransactionalImpl.job;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.common.json.JSON;

import cn.eeepay.framework.model.BossOperLog;
import cn.eeepay.framework.model.ShareDeductionSet;
import cn.eeepay.framework.service.BossOperLogService;
import cn.eeepay.framework.service.BossSysConfigService;
import cn.eeepay.framework.service.unTransactionalImpl.abstractJob.ScheduleJob;
import cn.eeepay.framework.util.DateUtils;

@Component
public class ShareDeductionUpdateValueJob extends ScheduleJob {


    @Resource
    private BossSysConfigService bossSysConfigService;
	@Autowired
	private BossOperLogService bossOperLogService;

	public static final String FUNCTION_MANAGE_076_MODIFY = "FUNCTION_MANAGE_076_MODIFY";

	public static final String FUNCTION_MANAGE_076 = "FUNCTION_MANAGE_076";

	@Override
	public void runTask(String runNo) {
		String modifyValue = bossSysConfigService.selectValueByKey(FUNCTION_MANAGE_076_MODIFY);
		if (StringUtils.isEmpty(modifyValue)) {
			return;
		}

		try {
			ShareDeductionSet modifyShareDeductionSet = JSON.parse(modifyValue, ShareDeductionSet.class);
			String modifyRate = modifyShareDeductionSet.getRate();
			if (StringUtils.isNotEmpty(modifyRate)) {
				Date currentDate = new Date();
				
				String oldValue = bossSysConfigService.selectValueByKey(FUNCTION_MANAGE_076);
				modifyShareDeductionSet.setEffectiveDate(DateUtils.format(currentDate, DateUtils.DATATIMEFORMAT));
				modifyShareDeductionSet.setRate(modifyRate);
				String json = JSON.json(modifyShareDeductionSet);
				bossSysConfigService.updateValueByKey(FUNCTION_MANAGE_076, json);
				
				// 记录日志
				BossOperLog bossOperLog = new BossOperLog();
				bossOperLog.setUser_id(0);
				bossOperLog.setUser_name("ShareDeductionUpdateValueJob");
				bossOperLog.setRequest_method("runTask");
				bossOperLog.setRequest_params(oldValue);
				bossOperLog.setReturn_result(json);
				bossOperLog.setMethod_desc("分润扣税比例更新定时任务");
				bossOperLog.setOper_status(1);
				bossOperLogService.insert(bossOperLog);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
